// pages/my/my.js

var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */

  data: {
      address:'',
      latitude:'',
      longitude:'',
      province:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'PEWBZ-TGV3O-MVQWT-SRGBC-HRRAO-HVBGJ'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let vm = this;
      vm.getUserLoc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  formSubmit: function (e) {
    console.log("form发生了submit事件，携带数据：", e.detail.value)
    wx.request({
      url: 'http://localhost:3000/from_get?',
      data:
      {
        item1: e.detail.value.first,
        item2: e.detail.value.second
      },
      success: (res) => {
        console.log(res);
        if (res.data.ret.item3 == "true") {
          console.log("login success")
          wx.navigateTo({
            url: '../chat/chat',
          })
          wx.setNavigationBarTitle({
            title: '客服服务',
          })
        }
        else {
          console.log("login fail, please try again");
          wx.showToast({
            title: '账号或密码错误',
            duration: 2000,
            mask: true,
            icon: 'fail'
          })
        }
      }
    })
  },

  getLocation:function(){
    let vm = this;
    wx.getLocation({
      type:'gcj02',
      success: function(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        vm.getLocalInfo(latitude,longitude);
      },
      fail: function (res) {
        console.log("fail" + JSON.stringify(res))
      }
    })
  },

  getLocalInfo:function(lati,long){
      let vm = this;
    qqmapsdk.reverseGeocoder({
      //位置坐标，默认获取当前位置，非必须参数
       //Object格式
        location: {
          latitude: lati,
          longitude: long
        },
        success:function(res){
            let prov = res.result.ad_info.province
          let addre = res.result.formatted_addresses.recommend
          vm.setData({
            province:prov,
            address:addre,
            latitude:lati,
            longitude:long
          })
        },
        fail:function(res){
            console.log("位置存储失败"+res)
        }
    })

  },

  getUserLoc:function(){
    let vm = this
    wx.getSetting({
      success(res){
        console.log(res.authSetting)
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true){
            wx.showModal({
              title: '请求当前位置',
              content: '需要获取您的当前位置，请确认授权！',
              success(res){
                if(res.cancel){
                  wx.showToast({
                    title: '拒绝授权',
                    icon:'none',
                    duration:1000
                  })
                }else if(res.confirm){
                  wx.openSetting({
                    success:function(res){
                      if (res.authSetting['scope.userLocation'] == true){
                        wx.showToast({
                          title: '授权成功',
                          icon:'none',
                          duration:1000
                        })
                        vm.getLocation();
                      }else{
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                          duration: 1000
                        })
                      }
                    }
                  })
                }
              }
            })
        } else if (res.authSetting['scope.userLocation'] == undefined){
          vm.getLocation();
        }else{
          vm.getLocation();
        }
      }
    })
  }
})